package com.oujiong.iplimiter.aop.limit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;


import java.util.List;

@Service
public class CallLuaService {
    final static Logger log = LoggerFactory.getLogger(CallLuaService.class);

    @Autowired
    JedisPool jedisPool;
    /**
     *
     * @param luaStr  lua 脚本
     * @param keyArr  redis中键列表
     * @param argArr  参数列表
     * @return obj
     */
    public Object call(String luaStr, List<String> keyArr,List<String> argArr){
        Object result = null;
        Jedis jedis =null;
        try {
            jedis = jedisPool.getResource();
            result = jedis.eval(luaStr,keyArr, argArr);
        } catch (Exception e) {
            log.error("出错：调用lua 脚本;luaStr:{},keyArr:{},argArr:{}",luaStr,keyArr,argArr);
        } finally {
            if(jedis!=null) jedis.close();
        }
        return result;
    }
}
